package com.zxc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxc.dao.CstCustomerMapper;
import com.zxc.dao.CstLinkmanMapper;
import com.zxc.dto.CustomerConstitute;
import com.zxc.entity.CstCustomer;
import com.zxc.service.CstCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CstCustomerServiceImpl  extends ServiceImpl<CstCustomerMapper,CstCustomer> implements CstCustomerService {

    @Autowired
    private CstCustomerMapper cstCustomerMapper;

    @Autowired
    private CstLinkmanMapper cstLinkmanMapper;

    @Override
    public CstCustomer selectCstCustomerById(Integer custNo) {
        return cstCustomerMapper.selectById(custNo);
    }

    @Override
    public List<CstCustomer> getCstCustomerList(Integer skipCounts,Integer pageSize,String searchName) {
        List<CstCustomer> cstCustomerList = cstCustomerMapper.getCstCustomerList(skipCounts,pageSize,searchName);
        if (searchName != null){
            System.out.println(searchName);
            System.out.println(cstCustomerList);
        }else {
            System.out.println("searchName为null");
        }
        return cstCustomerList;
    }

    @Override
    public int selectCountCustomerForMarketing() {
        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cust_manager_id",0);
        return cstCustomerMapper.selectCount(queryWrapper);
    }

    @Override
    public int selectCountCustomerForCustomer() {
        //这个方法不需要根据客户经理Id来进行查询
        return cstCustomerMapper.selectCount(new QueryWrapper<>());
    }

    @Override
    @Transactional
    public int saveCstCustomer(CstCustomer cstCustomer) {
        int result = cstCustomerMapper.insert(cstCustomer);
        return result;
    }

    @Override
    @Transactional
    public int updateCustomerByCustNo(CstCustomer cstCustomer) {
        int result = cstCustomerMapper.updateById(cstCustomer);
        return result;
    }

    @Override
    @Transactional
    public int delete(Integer[] custNos) {
        List<Integer> custNoList = new ArrayList<>();
        for (Integer custNo : custNos){
            custNoList.add(custNo);
            //删除对应的联系人表里面的信息
            Map<String,Object> modelMap = new HashMap<>();
            modelMap.put("lkm_cust_no",custNo);
            int result = cstLinkmanMapper.deleteByMap(modelMap);
            if (result <= 0){
                throw new RuntimeException("删除客户联系人信息失败");
            }
        }
        int result = cstCustomerMapper.deleteBatchIds(custNoList);
        return result;
    }

    @Override
    public List<CstCustomer> getCstCustomerListByCondition(int skipCounts, Integer pageSize, String searchName) {
        List<CstCustomer> cstCustomerList = cstCustomerMapper.getCstCustomerListByCondition(skipCounts,pageSize,searchName);
        if (searchName != null){
            System.out.println(searchName);
            System.out.println(cstCustomerList);
        }else {
            System.out.println("searchName为null");
        }
        return cstCustomerList;
    }

    @Override
    public List<CstCustomer> getCstCustomerListByConditionAndUserId(int skipCounts, Integer pageSize, String searchName,Integer userId) {
        List<CstCustomer> cstCustomerList = cstCustomerMapper.getCstCustomerListByConditionAndUserId(skipCounts,pageSize,searchName,userId);
        if (searchName != null){
            System.out.println(searchName);
            System.out.println(cstCustomerList);
        }else {
            System.out.println("searchName为null");
        }
        return cstCustomerList;
    }

    @Override
    public Long selectCstCustomerByCustName(String svrCustName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cust_name",svrCustName);
        CstCustomer cstCustomer = cstCustomerMapper.selectOne(queryWrapper);
        return cstCustomer.getCustNo();
    }

    @Override
    public List<CstCustomer> selectAllCustomer() {
        List<CstCustomer> cstCustomerList = cstCustomerMapper.selectList(new QueryWrapper<>());
        return cstCustomerList;
    }

    @Override
    public List<CstCustomer> selectCstCustomerByCustNameForChart(String custName) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("cust_name",custName);
        List<CstCustomer> cstCustomerList = cstCustomerMapper.selectList(queryWrapper);
        return null;
    }

    @Override
    public List<CustomerConstitute> getCustomerConstituteInit() {
        List<CustomerConstitute> customerConstituteList = cstCustomerMapper.selectCstCustomerConstitute();
        System.out.println("查询到的list="+customerConstituteList);

        return customerConstituteList;
    }

    //取得企业客户等级列表
    @Override
    public List<String> selectCustLevel() {
        List<String> custLevelList = cstCustomerMapper.selectCustLevel();
        return custLevelList;
    }

    @Override
    public List<String> getCustomerByCustName(String keyword) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("cust_name",keyword);
        List<CstCustomer> cstCustomerList = cstCustomerMapper.selectList(queryWrapper);
        List<String> stringList = new ArrayList<>();
        for (CstCustomer c : cstCustomerList){
            stringList.add(c.getCustName());
        }
        System.out.println("模糊查询出来的名称="+stringList);
        return stringList;
    }
}
